package net.betou.trade.web;

import org.apache.commons.lang.StringUtils;


public class WebErrors extends net.betou.common.web.springmvc.WebErrors{
	
	public static WebErrors create( ){
		return new WebErrors( );
	}
	
	public WebErrors( ){
		super( );
	}
	
	public String getError( ){
		java.util.List< String > errors = getErrors( );
		if( errors.size( ) > 0 ){
			return errors.get( 0 );
		}
		return "";
	}
	
	/**
	 * 直接添加错误消息，不经过国际化
	 */
	public void addError( String code ){
		this.getErrors( ).add( code );
	}
	
	@Override
	public boolean ifBlank( String s, String field, int maxLength ) {
		
		if( StringUtils.isBlank( s ) ){
			addError( field + "为必填项"  );
			return true;
		}
		if( ifMaxLength( s, field, maxLength ) ){
			return true;
		}
		return false;
		
	}

	@Override
	public boolean ifEmpty( Object [ ] o, String field ) {
		if( o == null || o.length <= 0 ){
			addError( field + "为必填项"  );
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean ifMaxLength( String s, String field, int maxLength ) {
		if( s != null && s.length( ) > maxLength ){
			addError( field + "的长度不能大于" + maxLength );
			return true;
		}
		return false;
		
	}

	@Override
	public boolean ifNull( Object o, String field ) {
		if( o == null ){
			addError( field + "为必填项"  );
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public boolean ifOutOfLength( String s, String field, int minLength, int maxLength ) {
		if( s == null ){
			addError( field + "为必填项"  );
			return true;
		}
		int len = s.length( );
		if( len < minLength || len > maxLength ){
			addError( field + "的长度必须在" + minLength + "和" + maxLength + "之间" );
			return true;
		}
		return false;
	}






	/**
	 * 默认错误页面
	 */
	public static final String ERROR_PAGE = "/common/trade_error_message";
	/**
	 * 默认错误信息属性名称
	 */
	public static final String ERROR_ATTR_NAME = "errors";
	
	@Override
	protected String getErrorAttrName( ) {
		return ERROR_ATTR_NAME;
	}

	@Override
	protected String getErrorPage( ) {
		return ERROR_PAGE;
	}

}
